import _ from 'underscore';
import { merge } from '../../helper/helper';
import { typeList, allContacts, allDeals, typeMap } from '../static/index';
import moment from 'moment';

const defaultState = {
    newQueue: {
        isAddQueue: false,
        name: '',
        trigger: null
    },
    queueList: [],
    taskList: {
        isPending: false,
        skip: 0,
        count: 0,
        chosen: [0, 1, 2, 3, 4, 5, 6,7,8],/////决定有几个thead
        orderBy: 'goodsPrice',
        data: [],
        checked: [],
        currentPageIndex: 0,
        targetPageIndex: 0,
        direction: 'DESC',
        pathname: 'tasks',
        time: '',
    },
    contacts: {
        options: allContacts
    },
    deals: {
        deals: allDeals
    },
    users: [],
    doneChecked: [],
    newTask: {
        isPending: false,
        modalType: 'new',
        isAddTask: false,
        isDelete: false,
        wfTaskName: '', //任务名称
        goodsPrice: '',
        goodsName: '',
        goodsUrl: '',
        goodsUrl1: '',
        goodsPic: '',
        goodsId: '',    
        goodsDesc: '',
        goodsDesc1:'',
        commendStatus:'',
        taskType: {}
    },
    visible: false,
    switchOn:false
}


function reducer(state = defaultState, action) {
    switch (action.type) {
        case 'trigger-update-goods' :
            if (action.type=='trigger-update-goods') {
                return merge(state, {
                    newTask: {
                        isAddTask: !state.newTask.isAddTask,
                        modalType: 'update',
                        wfTaskName: '',
                        goodsName:action.val.goodsName,
                        goodsId:action.val.goodsId,
                        goodsDesc: action.val.goodsDesc,
                        goodsUrl:action.val.goodsPic,
                        goodsPrice:action.val.goodsPrice,
                        discountPrice:action.val.discountPrice,
                        discountRate:action.val.discountRate,
                        commendStatus:1 
                    }
                })
        }
        case 'trigger-add-goods':
        if (!action.val) {
            return merge(state, {
                newTask: {
                    isAddTask: !state.newTask.isAddTask,
                    modalType: 'add',
                    wfTaskName: '',
                    goodsUrl:''
                }
            })
            } else {
                var data = action.val;
                console.log('进了89行')
                return merge(state, {
                    newTask: merge(state.newTask, {
                        isAddTask: !state.newTask.isAddTask,
                        modalType: 'edit',
                        wfTaskId: data.wfTaskId,
                        wfTaskName: data.wfTaskName,
                        customer: { customerId: data.customerId || "", customerName: data.customerName || "" },
                        deal: { billNo: data.billNo || "" },
                        targetUser: { employeId: data.targetUser || "", employeName: data.targetUserName || "" },
                        taskType: { id: data.taskType || "", name: typeMap[data.taskType] || "" },
                        taskGroup: { wf_taskgroup_id: data.taskGroup || "", wf_taskgroup_name: data.taskGroupName || "" },
                        endTime: data.endTime,
                        remindTime: data.remindTime

                    })
                })
            }
        //////////活动模态框
        case 'trigger-showModal':
            return merge(state,{
                visible:true
            })
        case 'trigger-hideModal':
            return merge(state,{
                visible:false
            })
        case 'trigger-cancelModal':
            return merge(state,{
                visible:false
            })
        ////////活动模态框结束
        case 'trigger-tuiJian':
            console.log(state.switchOn)
            return merge(state,{
                switchOn:!state.switchOn
            })
        case 'add-submit':
        console.log(action.val)
            return merge(state, {
                newTask: {
                    isAddTask: !state.newTask.isAddTask,
                    modalType: 'add',
                    wfTaskName: '',
                    goodsUrl:action.val.goodsUrl
                }
            })
        case 'task-list-render-data':
            return merge(state, {
                taskList: merge(state.taskList, {
                    isPending: false,
                    data: action.val.data,
                    count: action.val.total
                })
            })
        case 'task-list-request':
            return merge(state, {
                taskList: merge(state.taskList, merge(action.val, {
                    isPending: true
                }))
            });
        case 'task-list-set-setPageIndex':
            return merge(state, {
                taskList: merge(state.taskList, {
                    currentPageIndex: action.val,
                    targetPageIndex: action.val
                })
            });
        case 'task-list-set-chosen':
            return merge(state, {
                taskList: merge(state.taskList, {
                    chosen: action.val
                })
            });
        case 'task-list-set-checked':
            return merge(state, {
                taskList: merge(state.taskList, {
                    checked: action.val
                })
            });
        case 'set-done-checked':
            return merge(state, {
                doneChecked: action.val
            })
        case 'trigger-change-time':
            return merge(state, {
                doneChecked: [],
                taskList: merge(state.taskList, {
                    time: Date.now(),
                    checked: []
                })
            })


        case 'set-task-type':
            return merge(state, {
                newTask: merge(state.newTask, {
                    taskType: action.val
                })
            })
        case 'set-task-queue':
            return merge(state, {
                newTask: merge(state.newTask, {
                    taskGroup: action.val
                })
            })
        case 'set-task-user':
            return merge(state, {
                newTask: merge(state.newTask, {
                    targetUser: action.val
                })
            })
        case 'set-due-date':
            return merge(state, {
                newTask: merge(state.newTask, {
                    endTime: action.val
                })
            })
        case 'set-remind-date':
            return merge(state, {
                newTask: merge(state.newTask, {
                    remindTime: action.val
                })
            })
        case 'change-newtask-pic':
            ////////////// 商品封面图片选择后的reducer
            return merge(state, {
                newTask: merge(state.newTask, {
                    profile_image: action.val.profile_images,  //file数组
                    profile_image_temp: action.val.profile_image_temps, 
                    type: action.val.type,
                })
            })
            
        case 'change-newtask-pic_d':
            ////////////// 商品详情图片选择后的reducer
            return merge(state, {
                newTask: merge(state.newTask, {
                    profile_images_d:action.val.profile_images_d ,
                    profile_image_detail: action.val.profile_image_detail,/// base64,img的src 数组
                    type: action.val.type,
                })
            })
            
        case 'submit-newtask-pic':
            return merge(state, {
                newTask: merge(state.newTask, {
                    profile_image: action.val.profile_image,
                    profile_image_temp: action.val.profile_image_temp,
                    type: action.val.type
                })
            })
        case 'change-task-discounutRate':
            return merge(state, {
                newTask: merge(state.newTask, {
                    wfTaskName: action.val,
                    discountRate: action.val,
                })
            })
        case 'change-task-discounut':
            return merge(state, {
                newTask: merge(state.newTask, {
                    wfTaskName: action.val,
                    discountPrice: action.val,
                })
            })
        case 'change-task-commendStatus':
            return merge(state, {
                newTask: merge(state.newTask, {
                    wfTaskName: action.val,
                    commendStatus: action.val,
                })
            })
        case 'change-task-title':
            return merge(state, {
                newTask: merge(state.newTask, {
                    wfTaskName: action.val,
                    goodsPrice: action.val,
                })
            })
        case 'change-task-desc':
            return merge(state, {
                newTask: merge(state.newTask, {
                    desc: action.val,
                    goodsDesc: action.val,
                })
            })
        case 'change-task-result':
            return merge(state, {
                newTask: merge(state.newTask, {
                    result: action.val,
                    goodsUrl: action.val
                })
            })
        case 'change-task-feedback':
            return merge(state, {
                newTask: merge(state.newTask, {
                    feedback: action.val,
                    goodsName: action.val
                })
            })
        case 'change-task-procedding':
            return merge(state, {
                newTask: merge(state.newTask, {
                    procedding: action.val
                })
            })
        case 'set-task-associate-contact':
            return merge(state, {
                newTask: merge(state.newTask, {
                    customer: action.val
                })
            })
        case 'set-task-associate-deal':
            return merge(state, {
                newTask: merge(state.newTask, {
                    deal: action.val
                })
            })
        case 'trigger-set-pending':
            return merge(state, {
                isPending: !state.isPending
            })
        case 'trigger-delete-task':
            return merge(state, {
                newTask: merge(state.newTask, {
                    isDelete: !state.newTask.isDelete
                })
            })
        case 'set-path-name':
            if (state.taskList.pathname !== action.val) {
                // 不相等则是切换筛选条件， 否则不需要做操作
                return merge(state, {
                    taskList: merge(state.taskList, {
                        pathname: action.val,
                        skip: 0,
                        orderBy: 'deadline',
                        data: [],
                        currentPageIndex: 0,
                        targetPageIndex: 0,
                    })
                })
            }
            break;
        case 'set-all-queues':
            return merge(state, {
                queueList: action.val
            })
        case 'trigger-add-queue':
            return merge(state, {
                newQueue: merge(state.newQueue, {
                    isAddQueue: !state.newQueue.isAddQueue,
                    name: ''
                })
            })
        case 'set-queue-name':
            return merge(state, {
                newQueue: merge(state.newQueue, {
                    name: action.val
                })
            })
        case 'change-queue-trigger':
            return merge(state, {
                newQueue: merge(state.newQueue, {
                    trigger: action.val
                })
            })
    }

    return state;
}

export default reducer;